package com.weitu.permission.dictionaryData.service.impl;

import com.weitu.permission.dictionaryData.entity.DictionaryData;
import com.weitu.permission.dictionaryData.mapper.DictionaryDataMapper;
import com.weitu.permission.dictionaryData.service.DictionaryDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/6/15.
 */
@Service
public class DictionaryDataServiceImpl implements DictionaryDataService {
    private final DictionaryDataMapper dictionaryDataMapper;

    @Autowired
    public DictionaryDataServiceImpl(DictionaryDataMapper dictionaryDataMapper){
        this.dictionaryDataMapper=dictionaryDataMapper;
    }


    @Override
    public int queryCount(Map<String, Object> map) {
        return this.dictionaryDataMapper.queryCount(map);
    }

    @Override
    public List<DictionaryData> queryList(Map<String, Object> map) {
        return this.dictionaryDataMapper.queryList(map);
    }

    @Override
    public List<DictionaryData> queryTreeList(Map<String, Object> map) {
        return this.dictionaryDataMapper.queryTreeList(map);
    }

    @Override
    public int queryCountByCode(String code) {
        return this.dictionaryDataMapper.queryCountByCode(code);
    }

    @Override
    public int insert(DictionaryData dictionaryData) {
        return this.dictionaryDataMapper.insert(dictionaryData);
    }


    @Override
    @Transactional
    public int delete(String id) {
        int num=0;
        return deleteByPId(id,num);
    }

    private int deleteByPId(String pId,int num){
        List<DictionaryData> dataList=queryListAll(pId);
        for (int i=0;i<dataList.size();i++){
           num+= deleteByPId(dataList.get(i).getId(),num);
        }
        num+=this.dictionaryDataMapper.delete(pId);
        return num;
    }


    @Override
    public DictionaryData queryById(String id) {
        return this.dictionaryDataMapper.queryById(id);
    }

    @Override
    public int update(DictionaryData dictionaryData) {
        return this.dictionaryDataMapper.update(dictionaryData);
    }

    @Override
    public List<DictionaryData> queryDictionaryDataByParentCode(String code) {
        return this.dictionaryDataMapper.queryDictionaryDataByParentCode(code);
    }

    @Override
    public List<DictionaryData> queryListAll(String pId) {
        return this.dictionaryDataMapper.queryListAll(pId);
    }


    @Override
    public DictionaryData queryDictionaryDataByCode(String code) {
        return this.dictionaryDataMapper.queryDictionaryDataByCode(code);
    }
}
